<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body, html,#allmap {width: 720px;height: 1280px;overflow: hidden;margin:0;}
#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
#r-result{height:100%;width:20%;float:left;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
<script type="text/javascript" src="convertor.js"></script>
<title>地图</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
var map = new BMap.Map("allmap");
map.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_ZOOM}));
var tp;

function addPoint(lng, lat, type) {
var gpsPoint = new BMap.Point(lng, lat);
tp = type;
map.centerAndZoom(gpsPoint,14);
setTimeout(function(){
    BMap.Convertor.translate(gpsPoint,0,translateCallback);     //真实经纬度转成百度坐标
}, 2000);

}

function addMyPoint(lng, lat, type) {
var gpsPoint = new BMap.Point(lng, lat);
tp = type;
map.centerAndZoom(gpsPoint,14);
setTimeout(function(){
    BMap.Convertor.translate(gpsPoint,0,translateMyCallback);     //真实经纬度转成百度坐标
}, 2000);

}

//坐标转换完之后的回调函数
translateMyCallback = function (point){
    
    addMarker(point,tp,"我的位置");
    map.setCenter(point);
}

//坐标转换完之后的回调函数
translateCallback = function (point){
    addMarker(point,tp,"我的位置");
    map.setCenter(point);
    addMulPoint(point);
}

function addMarker(point, type, label){
  var marker = new BMap.Marker(point);
  var myIcon;
  var tp = type;
  if (tp == "0") {
  	myIcon = new BMap.Icon("red.png", new BMap.Size(22, 29), {offset: new BMap.Size(10, 29),imageOffset: new BMap.Size(0, 0)});    
  } else if (tp == "1") {
   myIcon = new BMap.Icon("green.png", new BMap.Size(22, 29), {offset: new BMap.Size(10, 29),imageOffset: new BMap.Size(0, 0)});    
  } else {
   myIcon = new BMap.Icon("orange.png", new BMap.Size(22, 29), {offset: new BMap.Size(10, 29),imageOffset: new BMap.Size(0, 0)});
  }
  marker.setIcon(myIcon);
  var label = new BMap.Label(label,{offset:new BMap.Size(20,-10)});
  marker.setLabel(label);
  map.addOverlay(marker);
}

function addMulPoint(point) {
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var lngSpan = Math.abs(sw.lng - ne.lng);
var latSpan = Math.abs(ne.lat - sw.lat);
for (var i = 0; i < 9; i ++) {

var ralng = sw.lng + lngSpan * (Math.random() * 0.7);
var ralat = ne.lat - latSpan * (Math.random() * 0.7);
var newpoint = new BMap.Point(ralng, ralat);
//var polyline = new BMap.Polyline([
// point,
//  newpoint
//], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});

var dis = map.getDistance(point,newpoint);
dis = format_number(dis,1);
var msg = "距我" + dis + "公里";
//map.addOverlay(polyline);


if (i > 5) {
addMarker(newpoint, "0",msg);
} else {
addMarker(newpoint, "1",msg);
}


}
}

/**
 * 格式化数字，保留小数点后n位小数
 * @param src-源数字
 * @param n-小数点后保留的位数
 * @returns {String}
 */
function format_number(srcNumber, n){
	var dstNumber = parseFloat(srcNumber);
	dstNumber = dstNumber / 1000;
	if(isNaN(dstNumber)){
		return srcNumber;
	}
	if(dstNumber>=0){
		dstNumber = parseInt(dstNumber*Math.pow(10, n)+0.5)/Math.pow(10, n);
	}else{
		var tmpDstNumber = -dstNumber;
		dstNumber = parseInt(tmpDstNumber*Math.pow(10, n)+0.5)/Math.pow(10, n);
	}
 
	var dstStrNumber = dstNumber.toString();
	var dotIndex = dstStrNumber.indexOf('.');
	if(dotIndex<0){
		dotIndex = dstStrNumber.length;
		dstStrNumber += '.';
	}
	while(dstStrNumber.length <= dotIndex+n){
		dstStrNumber += '0';
	}
 
	return dstStrNumber;
}

//addPoint(121.54, 38.88,3);
</script>